package com.beehoo.dcportal.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.beehoo.dcportal.common.base.BaseDao;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.dao.ICommonDao;
import com.beehoo.dcportal.model.MenuBean;
import com.beehoo.dcportal.model.PortletBean;
import com.beehoo.dcportal.model.StreetBean;
import com.beehoo.dcportal.model.ThemeBean;

public class CommonDaoImpl extends BaseDao implements ICommonDao {
	private static final Logger logger = Logger.getLogger(CommonDaoImpl.class);
	
	/**
	 * 获取中心的导航菜单
	 * @param streetId
	 * @return
	 * @throws BaseException
	 */
	@SuppressWarnings("unchecked")
	public List<MenuBean> queryNavMenu(String streetId) throws BaseException {
		List<MenuBean> menuLs = new ArrayList<MenuBean>();
		
		try {
			menuLs = getSqlMapClient().queryForList("Common.queryNavMenu", streetId);
			logger.info("初始化页面-查询菜单|#[中心编号："+streetId+"],成功!");
		} catch (SQLException e) {
			logger.error("初始化页面-查询菜单|#[中心编号："+streetId+"],失败!异常："+e.getMessage());
			throw new BaseException(e,"查询菜单失败");
		}
		
		return menuLs;
	}
	

	/**
	 * 查询特殊菜单
	 * @param paramMap
	 * @return
	 * @throws BaseException
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MenuBean> querySpecialMenu(Map<String, Object> paramMap)
			throws BaseException {
		List<MenuBean> menuLs = null;
		try {
			menuLs = getSqlMapClient().queryForList("Common.querySpecialMenu", paramMap);
			logger.info("首页Portlet-查询特殊菜单|#" +
					"[中心编号：" + paramMap.get("streetId")+"]," +
					"[父级特殊菜单编号：" + paramMap.get("parentMenuSpecial")+"]" + ",成功!");
		} catch (SQLException e) {
			logger.error("首页Portlet-查询特殊菜单|#" +
					"[中心编号：" + paramMap.get("streetId")+"]," +
					"[父级特殊菜单编号：" + paramMap.get("parentMenuSpecial")+"]" +
							"失败!异常："+e.getMessage());
			throw new BaseException(e,"查询特殊菜单失败");
		}
		return menuLs;
	}
	
	/**
	 * 获取中心的页面组件
	 * @param streetId
	 * @return
	 * @throws BaseException
	 */
	@SuppressWarnings("unchecked")
	public List<PortletBean> queryPortlets(String streetId, String themeId) throws BaseException {
		List<PortletBean> menuLs = new ArrayList<PortletBean>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("themeId", themeId);
		
		try {
			menuLs = getSqlMapClient().queryForList("Common.queryPortlet", paramMap);
			logger.info("初始化页面-查询页面组件|#[中心编号："+streetId+"],成功!");
		} catch (SQLException e) {
			logger.error("初始化页面-查询页面组件|#[中心编号："+streetId+"],失败!异常："+e.getMessage());
			throw new BaseException(e,"查询页面组件失败");
		}
		return menuLs;
	}
	
	/**
	 * 获取中心的页面主题
	 * @param streetId
	 * @return
	 * @throws BaseException
	 */
	public ThemeBean queryTheme(String streetId) throws BaseException {
		ThemeBean bean = new ThemeBean();
		try {
			bean = (ThemeBean) getSqlMapClient().queryForObject("Common.queryTheme", streetId);
			logger.info("初始化页面-查询页面主题|#[中心编号："+streetId+"],成功!");
		} catch (SQLException e) {
			logger.error("初始化页面-查询页面主题|#[中心编号："+streetId+"],失败!异常："+e.getMessage());
			throw new BaseException(e,"查询页面主题失败");
		}
		return bean;
	}

	/**
	 * 获取中心信息
	 * @param streetDomain
	 * @return
	 * @throws BaseException
	 */
	@Override
	public StreetBean queryStreet(String streetDomain) throws BaseException {
		StreetBean bean = new StreetBean();
		
		try {
			bean = (StreetBean) getSqlMapClient().queryForObject("Common.queryStreet", streetDomain);
			logger.info("初始化页面-查询中心信息|#[中心域名："+streetDomain+"],成功!");
		} catch (SQLException e) {
			logger.error("初始化页面-查询中心信息|#[中心域名："+streetDomain+"],失败!异常："+e.getMessage());
			throw new BaseException(e,"查询页面主题失败");
		}
		return bean;
	}

	@Override
	public String queryResidentIdByIDCard(String idCardNum)
			throws BaseException {
		String id = null;
		try {
			id = (String) getSqlMapClient().queryForObject("Common.queryResidentIDCard", idCardNum);
			logger.info("身份证验证-查询居民|#[身份证号码："+idCardNum+"],成功!");
		} catch (SQLException e) {
			logger.error("身份证验证-查询居民|#[身份证号码："+idCardNum+"],失败!异常："+e.getMessage());
			throw new BaseException(e,"custom.messages.error.idcard.num.error");
		}
		return id;
	}

	@Override
	public String queryWorderIdByIDCard(String idCardNum) throws BaseException {
		String id = null;
		try {
			id = (String) getSqlMapClient().queryForObject("Common.queryWorkerIDCard", idCardNum);
			logger.info("身份证验证-查询社工|#[身份证号码："+idCardNum+"],成功!");
		} catch (SQLException e) {
			logger.error("身份证验证-查询社工|#[身份证号码："+idCardNum+"],失败!异常："+e.getMessage());
			throw new BaseException(e,"custom.messages.error.idcard.num.error");
		}
		return id;
	}

}
